﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfAnalyzerLib;
using System.Drawing;

namespace PerfAnalyzerUI
{
	class DataGridViewHelpPerflogAnalysis : DataGridViewHelper
	{
		public const string ColColor = "col_PlAnalysis_Color";
		public const string ColLogFile = "col_PlAnalysis_LogFile";
		public const string ColMachine = "col_PlAnalysis_Machine";
		public const string ColCategory = "col_PlAnalysis_Category";
		public const string ColInstance = "col_PlAnalysis_Instance";
		public const string ColCounter = "col_PlAnalysis_Counter";
		public const string ColAverage = "col_PlAnalysis_Average";
		public const string ColMin = "col_PlAnalysis_Min";
		public const string ColMax = "col_PlAnalysis_Max";
		public const string ColStdDev = "col_PlAnalysis_StdDev";
		public const string ColPercentile25 = "col_PlAnalysis_Percentile25";
		public const string ColPercentile50 = "col_PlAnalysis_Percentile50";
		public const string ColPercentile75 = "col_PlAnalysis_Percentile75";
		public const string ColPercentile90 = "col_PlAnalysis_Percentile90";
		public const string ColPercentile95 = "col_PlAnalysis_Percentile95";
		public const string ColPercentile99 = "col_PlAnalysis_Percentile99";

		delegate void UpdateStatusColumnCallBack(DataGridViewRow row, string status);

		public DataGridViewHelpPerflogAnalysis(DataGridView trackedDataGridView, Form partentForm)
			: base(trackedDataGridView, partentForm)
		{
			_dataGridView.Columns[ColAverage].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColMin].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColMax].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColStdDev].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColPercentile25].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColPercentile50].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColPercentile75].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColPercentile90].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColPercentile95].DefaultCellStyle.Format = "N2";
			_dataGridView.Columns[ColPercentile99].DefaultCellStyle.Format = "N2";
		}

		public override DataGridViewRow AddRow(object tag)
		{
			Counter counter = (Counter)tag;

			_dataGridView.Rows.Add();
			int rowIndex = _dataGridView.RowCount - 1;
			DataGridViewRow row = _dataGridView.Rows[rowIndex];
			row.Cells[ColCategory].Value = counter.Info.Category;
			row.Cells[ColCounter].Value = counter.Info.Counter;
			row.Cells[ColInstance].Value = counter.Info.Instance;
			row.Cells[ColLogFile].Value = counter.Perflog.FileInfo.FullName;
			row.Cells[ColMachine].Value = counter.Info.Machine;
			row.Cells[ColAverage].Value = counter.Stats.Average;
			row.Cells[ColMax].Value = counter.Stats.Max;
			row.Cells[ColMin].Value = counter.Stats.Min;
			row.Cells[ColPercentile25].Value = counter.Stats.Percentile25th;
			row.Cells[ColPercentile50].Value = counter.Stats.Percentile50th;
			row.Cells[ColPercentile75].Value = counter.Stats.Percentile75th;
			row.Cells[ColPercentile90].Value = counter.Stats.Percentile90th;
			row.Cells[ColPercentile95].Value = counter.Stats.Percentile95th;
			row.Cells[ColPercentile99].Value = counter.Stats.Percentile99th;
			row.Cells[ColStdDev].Value = counter.Stats.StandardDeviation;

			row.Tag = counter;
			TagToRow.Add(counter, row);

			return row;
		}

		private void SetColumnValue(DataGridViewRow row, string columnName, double value)
		{
			row.Cells[columnName].Value = value.ToString("N2");
			row.Cells[columnName].Tag = value;
		}

		public override void AddRows(IEnumerable<object> tags)
		{
			foreach (var tag in tags)
			{
				AddRow(tag);
			}
		}

		public void SetRowColor(object tag, Color color)
		{
			TagToRow[tag].Cells[ColColor].Style.BackColor = color;
		}

		public Color GetRowColor(object tag)
		{
			return TagToRow[tag].Cells[ColColor].Style.BackColor;
		}

		public override void RemoveRow(DataGridViewRow row)
		{
			// TODO complete
			_dataGridView.Rows.Remove(row);
			TagToRow.Remove(row.Tag);
		}

		public void RemoveRowWithTag(object tag)
		{
			RemoveRow(TagToRow[(Counter)tag]);
		}

		public override void Clear()
		{
			// TODO complete
			_dataGridView.Rows.Clear();
			TagToRow.Clear();
		}
	}
}
